package cn.iocoder.yudao.module.biz.controller.admin.risk.hazardResource;

import cn.iocoder.yudao.module.biz.controller.admin.risk.hazardResource.vo.*;
import cn.iocoder.yudao.module.biz.convert.risk.hazardResource.BizHazardResourceConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.risk.hazardResource.BizHazardResourceDO;
import cn.iocoder.yudao.module.biz.service.risk.hazardResource.BizHazardResourceService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 危险源")
@RestController
@RequestMapping("/biz/hazard-resource")
@Validated
public class BizHazardResourceController {

    @Resource
    private BizHazardResourceService hazardResourceService;

    @PostMapping("/create")
    @ApiOperation("创建危险源")
    @PreAuthorize("@ss.hasPermission('biz:hazard-resource:create')")
    public CommonResult<String> createHazardResource(@Valid @RequestBody BizHazardResourceCreateReqVO createReqVO) {
        return success(hazardResourceService.createHazardResource(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新危险源")
    @PreAuthorize("@ss.hasPermission('biz:hazard-resource:update')")
    public CommonResult<Boolean> updateHazardResource(@Valid @RequestBody BizHazardResourceUpdateReqVO updateReqVO) {
        hazardResourceService.updateHazardResource(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除危险源")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:hazard-resource:delete')")
    public CommonResult<Boolean> deleteHazardResource(@RequestParam("id") String id) {
        hazardResourceService.deleteHazardResource(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得危险源")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:hazard-resource:query')")
    public CommonResult<BizHazardResourceRespVO> getHazardResource(@RequestParam("id") String id) {
        BizHazardResourceDO hazardResource = hazardResourceService.getHazardResource(id);
        return success(BizHazardResourceConvert.INSTANCE.convert(hazardResource));
    }

    @GetMapping("/list")
    @ApiOperation("获得危险源列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:hazard-resource:query')")
    public CommonResult<List<BizHazardResourceRespVO>> getHazardResourceList(@RequestParam("ids") Collection<String> ids) {
        List<BizHazardResourceDO> list = hazardResourceService.getHazardResourceList(ids);
        return success(BizHazardResourceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得危险源分页")
    @PreAuthorize("@ss.hasPermission('biz:hazard-resource:query')")
    public CommonResult<PageResult<BizHazardResourceRespVO>> getHazardResourcePage(@Valid BizHazardResourcePageReqVO pageVO) {
        PageResult<BizHazardResourceDO> pageResult = hazardResourceService.getHazardResourcePage(pageVO);
        return success(BizHazardResourceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出危险源 Excel")
    @PreAuthorize("@ss.hasPermission('biz:hazard-resource:export')")
    @OperateLog(type = EXPORT)
    public void exportHazardResourceExcel(@Valid BizHazardResourceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizHazardResourceDO> list = hazardResourceService.getHazardResourceList(exportReqVO);
        // 导出 Excel
        List<BizHazardResourceExcelVO> datas = BizHazardResourceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "危险源.xls", "数据", BizHazardResourceExcelVO.class, datas);
    }

}
